package com.bidevalution.business.checkJob;

import com.bidevalution.business.exception.CheckException;
import com.bidevalution.business.factory.CheckJobBase;
import com.bidevalution.business.service.BeBidResultService;
import com.bidevalution.business.service.BeSectionExpertInfoService;
import com.bidevalution.business.service.BeSectionWarnMsgService;
import com.bidevalution.dao.entity.BeSectionExpertInfo;
import com.bidevalution.dao.entity.CheckJob;
import com.bidevalution.dao.enums.CheckJobParamsEnum;
import com.bidevalution.support.context.Constants;
import com.qtp.core.util.GsonUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description 检查有效单位
 * @Date        2020-03-03 17:19
 * @Author      lhl
 */
@Log4j2
@Component
public class ValidUnitCheckJob extends CheckJobBase {

    /**
     * 小于多少家单位提醒
     */
    private static final int STANDARD_UNIT_NUM = 3;
    /**
     * 提醒标识
     */
    private static final String VALIDUNITCHECK_KEY = "ValidUnitCheck";
    /**
     * 上次有效单位数
     */
    private static final String PASS_UNIT_SUM = "pass_unit_sum";

    @Autowired
    private BeBidResultService beBidResultService;
    @Autowired
    private BeSectionWarnMsgService beSectionWarnMsgService;
    @Autowired
    private BeSectionExpertInfoService beSectionExpertInfoService;

    private static ValidUnitCheckJob rankCheckJob;

    @PostConstruct
    public void init() {
        rankCheckJob = this;
    }

    @Override
    public void check(CheckJob checkJob) throws Exception,CheckException {
        log.info("LOG[]check[]{},params={}", "检查有效单位",GsonUtil.toJsonWithGson(checkJob));
        try {
            String sectionStepId = checkJob.getSectionStepId();
            String sectionId = checkJob.getSectionId();
            int minUnitNum = Integer.parseInt(checkJob.getParam(CheckJobParamsEnum.MinUnitNum));
            String cloName = checkJob.getParam(CheckJobParamsEnum.ColName);
            Integer standardUnitNum = null;
            if (cloName.toLowerCase().startsWith("c")) {
                standardUnitNum = rankCheckJob.beBidResultService.getStandardUnitNum(sectionStepId, cloName);
            } else {
                standardUnitNum = rankCheckJob.beBidResultService.getStandardUnitNumByBusiness(sectionStepId);
            }

            boolean onlyManage = Boolean.valueOf(checkJob.getParam(CheckJobParamsEnum.OnlyManage));
            if(standardUnitNum != null && standardUnitNum < minUnitNum){
                Map<String, List<String>> notificationMap = new HashMap<>();
                // 初始全部专家
                for (BeSectionExpertInfo item : checkJob.getAllExperts()) {
                    String expId = item.getSeqId();
                    if (onlyManage) {
                        if (item.getIsManager() == Constants.ONE) {
                            notificationMap.put(expId, new ArrayList<>());
                        }
                    } else {
                        notificationMap.put(expId, new ArrayList<>());
                    }
                }

                // 幅值异常专家
                for (BeSectionExpertInfo item : checkJob.getAllExperts()) {
                    String expId = item.getSeqId();
                    notificationMap.get(expId).add(expId);
                }

                String title = String.format("企业少于%s", minUnitNum);
                throw new CheckException(title, notificationMap,
                        title + "，是否继续？",
                        title + "，请等待");
            }
        }catch (Exception e){
            log.error("LOG[]check[]{},error={},params={}","检查有效单位异常",GsonUtil.toJsonWithGson(e),GsonUtil.toJsonWithGson(checkJob));
            throw e;
        }
    }
}
